﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.Config.BL;
using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Inventory.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for ConfigurationSvs
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ConfigurationSvs : System.Web.Services.WebService
    {
        string XSLT_DIR = "xslt";

        [WebMethod]
        public string GetItems(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ItemDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM";
                dt.DataSet.DataSetName = "ITEMS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetItemLocationCapacities(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ItemLocationCapacityDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM_LOCATION_CAPACITY";
                dt.DataSet.DataSetName = "ITEM_LOCATION_CAPACITYS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetLocations(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = LocationDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "LOCATIONS";
                dt.DataSet.DataSetName = "LOCATION";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveItem(string stSessID, string stXML)
        {
            Session sess = null;
            ItemDO itemDO = null;
            ItemManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                itemDO = (ItemDO)XMLDocManager.Deserialize(typeof(ItemDO), XMLDocManager._XML_HEADER + stXML);
                mgr = ItemManager.GetInstance();
                if (itemDO.INTERNAL_ITEM_NUM > 0)
                    return mgr.SaveItem(sess, itemDO);
                else
                    return mgr.NewItem(sess, itemDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveLocations(string stSessID, string stFromLocXML, string stToLocXML)
        {
            Session sess = null;
            LocationDO fromLocDO = null;
            LocationDO toLocDO = null;
            LocationManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                fromLocDO = (LocationDO)XMLDocManager.Deserialize(typeof(LocationDO), XMLDocManager._XML_HEADER + stFromLocXML);
                toLocDO = (LocationDO)XMLDocManager.Deserialize(typeof(LocationDO), XMLDocManager._XML_HEADER + stToLocXML);

                mgr = new LocationManager();
                if (fromLocDO.INTERNAL_LOC_NUM > 0)
                    return mgr.UpdateLocations(sess, fromLocDO, toLocDO);
                else
                    return mgr.CreateLocations(sess, fromLocDO, toLocDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string SaveGenericConfig(string stSessID, string stXML)
        {
            Session sess = null;
            GenericConfigDetailDO cfgDO = null;
            ConfigHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                cfgDO = (GenericConfigDetailDO)XMLDocManager.Deserialize(typeof(GenericConfigDetailDO), XMLDocManager._XML_HEADER + stXML);
                helper = ConfigHelper.GetInstance();
                return helper.SaveGenericConfigDetail(sess, cfgDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetGenericConfigHeaders(string stSessID, string stFunctionGroup)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = GenericConfigHeaderDO.GetTableByColumn(sess, "FUNCTION_GROUP", stFunctionGroup);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "GENERIC_CONFIG_HEADER";
                dt.DataSet.DataSetName = "GENERIC_CONFIG_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetInventoryConfigHeaders(string stSessID)
        {
            return this.GetGenericConfigHeaders(stSessID, "INVENTORY");
        }

        [WebMethod]
        public string GetInboundConfigHeaders(string stSessID)
        {
            return this.GetGenericConfigHeaders(stSessID, "INBOUND");
        }

        [WebMethod]
        public string GetOutboundConfigHeaders(string stSessID)
        {
            return this.GetGenericConfigHeaders(stSessID, "OUTBOUND");
        }

        [WebMethod]
        public string GetBasicConfigHeaders(string stSessID)
        {
            return this.GetGenericConfigHeaders(stSessID, "BASIC");
        }

        [WebMethod]
        public string GetItemUOMs(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ItemUnitOfMeasureDO.GetTableByColumns(sess, dcCond, " ORDER BY SEQUENCE ASC");
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM_UNIT_OF_MEASURE";
                dt.DataSet.DataSetName = "ITEM_UNIT_OF_MEASURES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveItemUOM(string stSessID, string stXML)
        {
            Session sess = null;
            ItemUnitOfMeasureDO itemUOMDO = null;
            ItemManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                itemUOMDO = (ItemUnitOfMeasureDO)XMLDocManager.Deserialize(typeof(ItemUnitOfMeasureDO), XMLDocManager._XML_HEADER + stXML);
                mgr = new ItemManager();
                return mgr.SaveItemUOM(sess, itemUOMDO);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveItemBarcode(string stSessID, string stXML)
        {
            Session sess = null;
            ItemBarcodeDO itemBarcodeDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                itemBarcodeDO = (ItemBarcodeDO)XMLDocManager.Deserialize(typeof(ItemBarcodeDO), XMLDocManager._XML_HEADER + stXML);
                return "" + itemBarcodeDO.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetItemBarcodes(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ItemBarcodeDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM_BARCODE";
                dt.DataSet.DataSetName = "ITEM_BARCODES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }
       
        [WebMethod]
        public string SaveItemLocationCapacity(string stSessID, string stXML)
        {
            Session sess = null;
            ItemLocationCapacityDO itemLocCapDO = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam = null;
            DataTable dtCap = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                itemLocCapDO = (ItemLocationCapacityDO)XMLDocManager.Deserialize(typeof(ItemLocationCapacityDO), XMLDocManager._XML_HEADER + stXML);
                if (itemLocCapDO.INTERNAL_ITEM_CAPACITY_NUM > 0)
                {
                    itemLocCapDO.Update(sess);
                    return "" + itemLocCapDO.INTERNAL_ITEM_CAPACITY_NUM;
                }
                else
                {
                    //检查是否存在重复的货品货位容量设定
                    dcParam = new List<KeyValuePair<string,KeyValuePair<string,object>>>();
                    dcParam.Add(new KeyValuePair<string,KeyValuePair<string,object>>("WAREHOUSE", new KeyValuePair<string,object>("=", itemLocCapDO.WAREHOUSE)));
                    dcParam.Add(new KeyValuePair<string,KeyValuePair<string,object>>("COMPANY", new KeyValuePair<string,object>("=", itemLocCapDO.COMPANY)));
                    dcParam.Add(new KeyValuePair<string,KeyValuePair<string,object>>("ITEM", new KeyValuePair<string,object>("=", itemLocCapDO.ITEM)));
                    dcParam.Add(new KeyValuePair<string,KeyValuePair<string,object>>("LOCATION_TYPE", new KeyValuePair<string,object>("=", itemLocCapDO.LOCATION_TYPE)));
                    dtCap = ItemLocationCapacityDO.GetTableByColumns(sess, dcParam, null);
                    if (DataManager.IsEmpty(dtCap))
                        return "" + itemLocCapDO.Insert(sess);
                    else
                        return MessageManager.MSG_ITM_005;//错误：货位的库位容量设定已经存在，不能保存重复记录
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetLocationSelections(string stSessID, string stLocSelType)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = LocationSelectionDO.GetTableByColumn(sess, "LOC_SEL_TYPE", stLocSelType);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "LOCATION_SELECTION";
                dt.DataSet.DataSetName = "LOCATION_SELECTIONS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetLocationSelectionList(string stSessID, string stLocSelType)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                /*
                if (StringManager.IsEmpty(stLocSelType))
                    dt = LocationSelectionDO.GetAllToTable(sess);
                else
                    dt = LocationSelectionDO.GetTableByColumn(sess, "LOC_SEL_TYPE", stLocSelType);
                */
                dt = FilterConfigDetailDO.GetTableByColumn(sess, "RECORD_TYPE", stLocSelType);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "LOCATION_SELECTION";
                dt.DataSet.DataSetName = "LOCATION_SELECTIONS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return XMLDocManager.TransformStringWithFile(sbXML.ToString(), Server.MapPath(XSLT_DIR) + "/LocationSelectionList.xslt", Encoding.UTF8);
                //return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetLocatingRules(string stSessID)
        {
            //Dictionary<string, string> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = LocatingRuleHeaderDO.GetTableByColumn(sess, null, null);
                sbXML = new StringBuilder();
                dt.TableName = "LOCATING_RULE_HEADER";
                dt.DataSet.DataSetName = "LOCATING_RULE_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetWaveFlowHeaders(string stSessID)
        {
            //Dictionary<string, string> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = WaveFlowHeaderDO.GetTableByColumn(sess, null, null);
                sbXML = new StringBuilder();
                dt.TableName = "WAVE_FLOW_HEADER";
                dt.DataSet.DataSetName = "WAVE_FLOW_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetLocatingRuleDetails(string stSessID, string stLocRule, string stQtyUM, int iSeq)
        {
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new Dictionary<string, KeyValuePair<string, object>>();
                dcCond.Add("LOCATING_RULE", new KeyValuePair<string, object>("=",stLocRule));
                if (!StringManager.IsEmpty(stQtyUM))
                {
                    dcCond.Add("QUANTITY_UM", new KeyValuePair<string, object>("=",stQtyUM));
                }

                if (iSeq > 0)
                {
                    dcCond.Add("SEQUENCE", new KeyValuePair<string, object>("=",iSeq));
                }

                dt = LocatingRuleDetailDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), null);
                sbXML = new StringBuilder();
                dt.TableName = "LOCATING_RULE_DETAIL";
                dt.DataSet.DataSetName = "LOCATING_RULE_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetWaveFlowDetails(string stSessID, string stWaveFlow, int iSeq)
        {
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new Dictionary<string, KeyValuePair<string, object>>();
                dcCond.Add("WAVE_FLOW", new KeyValuePair<string, object>("=",stWaveFlow));

                if (iSeq > 0)
                {
                    dcCond.Add("SEQUENCE", new KeyValuePair<string, object>("=",iSeq));
                }

                dt = WaveFlowDetailDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), null);
                sbXML = new StringBuilder();
                dt.TableName = "WAVE_FLOW_DETAIL";
                dt.DataSet.DataSetName = "WAVE_FLOW_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveLocatingRuleDetail(string stSessID, string stXML)
        {
            Session sess = null;
            LocatingRuleDetailDO locRuleDtlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                locRuleDtlDO = (LocatingRuleDetailDO)XMLDocManager.Deserialize(typeof(LocatingRuleDetailDO), XMLDocManager._XML_HEADER + stXML);
                if (locRuleDtlDO.OBJECT_ID > 0)
                {
                    locRuleDtlDO.Update(sess);
                    return "" + locRuleDtlDO.OBJECT_ID;
                }
                else
                {
                    return "" + locRuleDtlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveLocatingRuleHeader(string stSessID, string stXML)
        {
            Session sess = null;
            LocatingRuleHeaderDO locRuleHdrlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                locRuleHdrlDO = (LocatingRuleHeaderDO)XMLDocManager.Deserialize(typeof(LocatingRuleHeaderDO), XMLDocManager._XML_HEADER + stXML);
                if (locRuleHdrlDO.OBJECT_ID > 0)
                {
                    locRuleHdrlDO.Update(sess);
                    return "" + locRuleHdrlDO.OBJECT_ID;
                }
                else
                {
                    return "" + locRuleHdrlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string SaveAllocationRuleDetail(string stSessID, string stXML)
        {
            Session sess = null;
            AllocationRuleDetailDO ruleDtlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                ruleDtlDO = (AllocationRuleDetailDO)XMLDocManager.Deserialize(typeof(AllocationRuleDetailDO), XMLDocManager._XML_HEADER + stXML);
                if (ruleDtlDO.OBJECT_ID > 0)
                {
                    ruleDtlDO.Update(sess);
                    return "" + ruleDtlDO.OBJECT_ID;
                }
                else
                {
                    return "" + ruleDtlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveAllocationRuleHeader(string stSessID, string stXML)
        {
            Session sess = null;
            AllocationRuleHeaderDO ruleHdrlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                ruleHdrlDO = (AllocationRuleHeaderDO)XMLDocManager.Deserialize(typeof(AllocationRuleHeaderDO), XMLDocManager._XML_HEADER + stXML);
                if (ruleHdrlDO.OBJECT_ID > 0)
                {
                    ruleHdrlDO.Update(sess);
                    return "" + ruleHdrlDO.OBJECT_ID;
                }
                else
                {
                    return "" + ruleHdrlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetAllocationRules(string stSessID)
        {
            //Dictionary<string, string> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = AllocationRuleHeaderDO.GetTableByColumn(sess, null, null);
                sbXML = new StringBuilder();
                dt.TableName = "ALLOCATION_RULE_HEADER";
                dt.DataSet.DataSetName = "ALLOCATION_RULE_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetAllocationRuleDetails(string stSessID, string stAllocRule, string stQtyUM, int iSeq)
        {
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new Dictionary<string, KeyValuePair<string, object>>();
                dcCond.Add("ALLOCATION_RULE", new KeyValuePair<string, object>("=", stAllocRule));
                if (!StringManager.IsEmpty(stQtyUM))
                {
                    dcCond.Add("QUANTITY_UM", new KeyValuePair<string, object>("=", stQtyUM));
                }

                if (iSeq > 0)
                {
                    dcCond.Add("SEQUENCE", new KeyValuePair<string, object>("=", iSeq));
                }

                dt = AllocationRuleDetailDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), null);
                sbXML = new StringBuilder();
                dt.TableName = "ALLOCATION_RULE_DETAIL";
                dt.DataSet.DataSetName = "ALLOCATION_RULE_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveLocationSelection(string stSessID, string stXML)
        {
            Session sess = null;
            LocationSelectionDO locSelDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                locSelDO = (LocationSelectionDO)XMLDocManager.Deserialize(typeof(LocationSelectionDO), XMLDocManager._XML_HEADER + stXML);
                if (locSelDO.OBJECT_ID > 0)
                {
                    locSelDO.Update(sess);
                    return "" + locSelDO.OBJECT_ID;
                }
                else
                {
                    return "" + locSelDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteLocatingRuleDetail(string stSessID, string stObjectID)
        {
            Session sess = null;
            LocatingRuleDetailDO locRuleDtlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                locRuleDtlDO = new LocatingRuleDetailDO();
                locRuleDtlDO.OBJECT_ID = Convert.ToInt32(stObjectID);
                locRuleDtlDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteLocatingRule(string stSessID, string stRule)
        {
            Session sess = null;
            LocatingRuleHeaderDO ruleHdrDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                ruleHdrDO = new LocatingRuleHeaderDO();
                ruleHdrDO.LOCATING_RULE = stRule;
                ruleHdrDO.Delete(sess);
                
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteLocationSelection(string stSessID, string stObjectID)
        {
            Session sess = null;
            LocationSelectionDO locSelDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                locSelDO = new LocationSelectionDO();
                locSelDO.OBJECT_ID = Convert.ToInt32(stObjectID);
                locSelDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteFilterDetail(string stSessID, int iObjID)
        {
            Session sess = null;
            ConfigHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new ConfigHelper();
                return helper.DeleteFilterDetail(sess, iObjID);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteGenericConfig(string stSessID, string stObjectID)
        {
            Session sess = null;
            GenericConfigDetailDO gcDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                gcDO = new GenericConfigDetailDO();
                gcDO.OBJECT_ID = Convert.ToInt32(stObjectID);
                gcDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveWaveFlowDetail(string stSessID, string stXML)
        {
            Session sess = null;
            WaveFlowDetailDO flowDtlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                flowDtlDO = (WaveFlowDetailDO)XMLDocManager.Deserialize(typeof(WaveFlowDetailDO), XMLDocManager._XML_HEADER + stXML);
                if (flowDtlDO.OBJECT_ID > 0)
                {
                    flowDtlDO.Update(sess);
                    return "" + flowDtlDO.OBJECT_ID;
                }
                else
                {
                    return "" + flowDtlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveWaveFlowHeader(string stSessID, string stXML)
        {
            Session sess = null;
            WaveFlowHeaderDO flowHdrlDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                flowHdrlDO = (WaveFlowHeaderDO)XMLDocManager.Deserialize(typeof(WaveFlowHeaderDO), XMLDocManager._XML_HEADER + stXML);
                if (flowHdrlDO.OBJECT_ID > 0)
                {
                    flowHdrlDO.Update(sess);
                    return "" + flowHdrlDO.OBJECT_ID;
                }
                else
                {
                    return "" + flowHdrlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string GetUsers(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = UserDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "USER";
                dt.DataSet.DataSetName = "USERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveUser(string stSessID, string stXML)
        {
            Session sess = null;
            UserDO userDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                userDO = (UserDO)XMLDocManager.Deserialize(typeof(UserDO), XMLDocManager._XML_HEADER + stXML);
                if (userDO.INTERNAL_USER_NUM > 0)
                {
                    userDO.Update(sess);
                    return "" + userDO.INTERNAL_USER_NUM;
                }
                else
                {
                    return "" + userDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string DeleteUser(string stSessID, string stUserName)
        {
            Session sess = null;
            UserDO userDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                for (int i = 0; i < Application.Count; i++)
                {
                    sess = (Session)Application[i];
                    if (StringManager.Equals(sess.User, stUserName))
                        return MessageManager.MSG_USR_004;//错误：用户处于登录状态，无法删除
                }

                userDO = new UserDO();
                userDO.USER_ID = stUserName;
                userDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }
        [WebMethod]
        public string GetReceivingPrefs(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ReceivingPreferenceDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "RECEIVING_PREFERENCE";
                dt.DataSet.DataSetName = "RECEIVING_PREFERENCES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveReceivingPref(string stSessID, string stXML)
        {
            Session sess = null;
            ReceivingPreferenceDO prefDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                prefDO = (ReceivingPreferenceDO)XMLDocManager.Deserialize(typeof(ReceivingPreferenceDO), XMLDocManager._XML_HEADER + stXML);
                if (prefDO.INTERNAL_PREF_NUM > 0)
                {
                    prefDO.Update(sess);
                    return "" + prefDO.INTERNAL_PREF_NUM;
                }
                else
                {
                    return "" + prefDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteReceivingPref(string stSessID, int iIntPrefNum)
        {
            Session sess = null;
            ReceivingPreferenceDO prefDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                prefDO = new ReceivingPreferenceDO();
                prefDO.INTERNAL_PREF_NUM = iIntPrefNum;
                prefDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteItemLocationCapacity(string stSessID, string iIntItemCapNum)
        {
            Session sess = null;
            ItemLocationCapacityDO targetDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                targetDO = new ItemLocationCapacityDO();
                targetDO.INTERNAL_ITEM_CAPACITY_NUM = Convert.ToInt32(iIntItemCapNum);
                targetDO.Delete(sess);

                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetCompanies(string stSessID)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = CompanyDO.GetAllToTable(sess);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "COMPANY";
                dt.DataSet.DataSetName = "COMPANIES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetCustomers(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = CustomerDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "CUSTOMER";
                dt.DataSet.DataSetName = "CUSTOMERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveCompany(string stSessID, string stXML)
        {
            Session sess = null;
            CompanyDO objDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                objDO = (CompanyDO)XMLDocManager.Deserialize(typeof(CompanyDO), XMLDocManager._XML_HEADER + stXML);
                if (objDO.INTERNAL_NUM > 0)
                {
                    objDO.Update(sess);
                    return objDO.COMPANY;
                }
                else
                {
                    return objDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveCustomer(string stSessID, string stXML)
        {
            Session sess = null;
            CustomerDO userDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                userDO = (CustomerDO)XMLDocManager.Deserialize(typeof(CustomerDO), XMLDocManager._XML_HEADER + stXML);
                if (userDO.INTERNAL_CUSTOMER_NUM > 0)
                {
                    userDO.Update(sess);
                    return "" + userDO.INTERNAL_CUSTOMER_NUM;
                }
                else
                {
                    return "" + userDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string GetVendors(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = VendorDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "VENDOR";
                dt.DataSet.DataSetName = "VENDORS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveVendor(string stSessID, string stXML)
        {
            Session sess = null;
            VendorDO userDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                userDO = (VendorDO)XMLDocManager.Deserialize(typeof(VendorDO), XMLDocManager._XML_HEADER + stXML);
                if (userDO.INTERNAL_VENDOR_NUM > 0)
                {
                    userDO.Update(sess);
                    return "" + userDO.INTERNAL_VENDOR_NUM;
                }
                else
                {
                    return "" + userDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }



        [WebMethod]
        public string GetTaskCreationMasters(string stSessID)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = TaskCreationMasterDO.GetAllToTable(sess);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "TASK_CREATION_MASTER";
                dt.DataSet.DataSetName = "TASK_CREATION_MASTERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), Server.MapPath(XSLT_DIR) + "/LocationSelectionList.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveTaskCreationMaster(string stSessID, string stXML)
        {
            Session sess = null;
            TaskCreationMasterDO tskMstDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                tskMstDO = (TaskCreationMasterDO)XMLDocManager.Deserialize(typeof(TaskCreationMasterDO), XMLDocManager._XML_HEADER + stXML);
                if (tskMstDO.INTERNAL_MASTER_NUM > 0)
                {
                    tskMstDO.Update(sess);
                    return "" + tskMstDO.INTERNAL_MASTER_NUM;
                }
                else
                {
                    return "" + tskMstDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string GetFieldsByTableName(string stSessID, string stTableName)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = SchemaRetrieval.GetFieldNamesForTable(sess, stTableName);
                sbXML = new StringBuilder();
                dt.TableName = "TABLE";
                dt.DataSet.DataSetName = "TABLES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetFilterStatements(string stSessID, string stRecordType, string stFilterName)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcParams = null;
            KeyValuePair<string, KeyValuePair<string, object>> dcParam;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcParams = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                dcParam = new KeyValuePair<string, KeyValuePair<string, object>>("RECORD_TYPE", new KeyValuePair<string, object>("=", stRecordType));
                dcParams.Add(dcParam); 
                dcParam = new KeyValuePair<string, KeyValuePair<string, object>>("FILTER_NAME", new KeyValuePair<string, object>("=", stFilterName));
                dcParams.Add(dcParam);
                dt = FilterStatementDO.GetTableByColumns(sess, dcParams, " ORDER BY SEQUENCE ASC");
                sbXML = new StringBuilder();
                dt.TableName = "STATEMENT";
                dt.DataSet.DataSetName = "STATEMENTS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveFilterStatement(string stSessID, string stRecordType, string stFilterName, string[] arXML)
        {
            Session sess = null;
            FilterStatementDO doObj = null;
            ConfigHelper helper = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                
                //删除目前的所有语句
                helper = new ConfigHelper();
                stErr = helper.DeleteFilterStatements(sess, stRecordType, stFilterName);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                //重新添加语句
                foreach (string stXML in arXML)
                {
                    doObj = (FilterStatementDO)XMLDocManager.Deserialize(typeof(FilterStatementDO), XMLDocManager._XML_HEADER + stXML);
                    doObj.Insert(sess);
                }

                //todo：更新filter_config_detail中的组合语句
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetFilterOrderBys(string stSessID, string stRecordType, string stFilterName)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcParams = null;
            KeyValuePair<string, KeyValuePair<string, object>> dcParam;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (!StringManager.IsEmpty(stRecordType))
                {
                    dcParams = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                    dcParam = new KeyValuePair<string, KeyValuePair<string, object>>("RECORD_TYPE", new KeyValuePair<string, object>("=", stRecordType));
                    dcParams.Add(dcParam);
                }
                if (!StringManager.IsEmpty(stFilterName))
                {
                    dcParam = new KeyValuePair<string, KeyValuePair<string, object>>("FILTER_NAME", new KeyValuePair<string, object>("=", stFilterName));
                    dcParams.Add(dcParam);
                }
                dt = FilterOrderByDO.GetTableByColumns(sess, dcParams, " ORDER BY SEQUENCE ASC");
                sbXML = new StringBuilder();
                dt.TableName = "ORDER_BY";
                dt.DataSet.DataSetName = "ORDER_BYS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveFilterOrderBy(string stSessID, string stRecordType, string stFilterName, string[] arXML)
        {
            Session sess = null;
            FilterOrderByDO doObj = null;
            ConfigHelper helper = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                //删除目前的所有语句
                helper = new ConfigHelper();
                stErr = helper.DeleteFilterOrderBy(sess, stRecordType, stFilterName);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                //重新添加语句
                foreach (string stXML in arXML)
                {
                    doObj = (FilterOrderByDO)XMLDocManager.Deserialize(typeof(FilterOrderByDO), XMLDocManager._XML_HEADER + stXML);
                    doObj.Insert(sess);
                }
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetFilterDetails(string stSessID, string stRecordType, string stFilterName)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcParams = null;
            KeyValuePair<string, KeyValuePair<string, object>> dcParam;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (!StringManager.IsEmpty(stRecordType))
                {
                    dcParams = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                    dcParam = new KeyValuePair<string, KeyValuePair<string, object>>("RECORD_TYPE", new KeyValuePair<string, object>("=", stRecordType));
                    dcParams.Add(dcParam);
                }
                if (!StringManager.IsEmpty(stFilterName))
                {
                    dcParam = new KeyValuePair<string, KeyValuePair<string, object>>("FILTER_NAME", new KeyValuePair<string, object>("=", stFilterName));
                    dcParams.Add(dcParam);
                }
                dt = FilterConfigDetailDO.GetTableByColumns(sess, dcParams, " ORDER BY RECORD_TYPE ASC, FILTER_NAME ASC");
                sbXML = new StringBuilder();
                dt.TableName = "FILTER";
                dt.DataSet.DataSetName = "FILTERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetFilterNameList(string stSessID, string stRecordType)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = FilterConfigDetailDO.GetFilterNameList(sess, stRecordType);
                if (DataManager.IsEmpty(dt))
                    return null;

                dt.DataSet.DataSetName = "NAMES";
                dt.TableName = "NAME";
                sbXML = new StringBuilder();
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SaveFilterDetail(string stSessID, string stRecordType, string stFilterName, string stXML)
        {
            Session sess = null;
            FilterConfigDetailDO doObj = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                doObj = (FilterConfigDetailDO)XMLDocManager.Deserialize(typeof(FilterConfigDetailDO), XMLDocManager._XML_HEADER + stXML);
                //从web页面传回的filter_statement只有where和order by子句，其余部分需要在这里补齐
                if (StringManager.Equals(doObj.RECORD_TYPE, "SHIP_ALLOC_TASK"))
                    doObj.FILTER_STATEMENT = "SELECT * FROM SHIPMENT_ALLOC_REQUEST WHERE INTERNAL_WAVE_NUM = @INTERNAL_WAVE_NUM " + doObj.FILTER_STATEMENT;
                else if (StringManager.Equals(doObj.RECORD_TYPE, "REPLENISH_TASK"))
                    doObj.FILTER_STATEMENT = "SELECT * FROM REPLENISHMENT_REQUEST WHERE INTERNAL_WAVE_NUM = @INTERNAL_WAVE_NUM " + doObj.FILTER_STATEMENT;
                else if (StringManager.Equals(doObj.RECORD_TYPE, "RECV_PUTAWAY_TASK"))
                    doObj.FILTER_STATEMENT = "SELECT * FROM RECEIPT_LOCATE_REQUEST WHERE INTERNAL_WAVE_NUM = @INTERNAL_WAVE_NUM " + doObj.FILTER_STATEMENT;
                else if (StringManager.Equals(doObj.RECORD_TYPE, "ALLOCATION_LOC_SEL"))
                    doObj.FILTER_STATEMENT = @"SELECT LOCATION_INVENTORY.* 
FROM LOCATION_INVENTORY, LOCATION 
WHERE LOCATION_INVENTORY.WAREHOUSE = LOCATION.WAREHOUSE 
	AND LOCATION_INVENTORY.LOCATION = LOCATION.LOCATION 
	AND LOCATION.LOCATION_CLASS = 'Inventory' 
    AND LOCATION_INVENTORY.ITEM = @ITEM
    AND LOCATION_INVENTORY.COMPANY = @COMPANY " + doObj.FILTER_STATEMENT; 
                else if (StringManager.Equals(doObj.RECORD_TYPE, "LOCATING_LOC_SEL"))
                    doObj.FILTER_STATEMENT = @"SELECT DISTINCT LOCATION.LOCATION  
FROM LOCATION LEFT JOIN LOCATION_INVENTORY    
	ON (LOCATION.WAREHOUSE = LOCATION_INVENTORY.WAREHOUSE AND LOCATION.LOCATION = LOCATION_INVENTORY.LOCATION) 
WHERE LOCATION.LOCATION_CLASS = 'Inventory' " + doObj.FILTER_STATEMENT; 
                if (doObj.OBJECT_ID > 0)
                {
                    doObj.Update(sess);
                    return "" + doObj.OBJECT_ID;
                }
                else
                    return "" + doObj.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetConfigValues(string stSessID, string stRecordType)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (!StringManager.IsEmpty(stRecordType))
                    dt = ConfigValueDO.GetTableByColumn(sess, "RECORD_TYPE", stRecordType);
                else
                    dt = ConfigValueDO.GetAllToTable(sess);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "CONFIG_VALUE";
                dt.DataSet.DataSetName = "CONFIG_VALUES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveConfigValue(string stSessID, string stXML)
        {
            Session sess = null;
            ConfigValueDO doObj = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                doObj = (ConfigValueDO)XMLDocManager.Deserialize(typeof(ConfigValueDO), XMLDocManager._XML_HEADER + stXML);
                if (doObj.OBJECT_ID > 0)
                {
                    doObj.Update(sess);
                    return "" + doObj.OBJECT_ID;
                }
                else
                    return "" + doObj.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string DeleteWaveFlow(String stSessID, int iObjectID)
        {

            Session sess = null;
            WaveFlowHeaderDO doObj = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                doObj = new WaveFlowHeaderDO();
                doObj.OBJECT_ID = iObjectID;
                doObj.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string DeleteWaveFlowDetail(String stSessID, int iObjectID)
        {

            Session sess = null;
            WaveFlowDetailDO doObj = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                doObj = new WaveFlowDetailDO();
                doObj.OBJECT_ID = iObjectID;
                doObj.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }
    }
}
